package org.yanfengying.gift.exception;

import org.yanfengying.gift.exception.code.ErrorCode;

import java.util.List;

public class MyGiftAssert {
    //定义一个全局异常常量
    public final static  String DEFAULT_MESSAGE = "非法请求!请输入相关信息后再操作!";

    //对字符串,对象,list进行不为空判断
    public static void assertNotNull(Object obj){//为空就抛出异常
        if(obj==null){
            throw new MyException(DEFAULT_MESSAGE);
        }
    }
    public static void assertNotNull(Object obj,String message){
        if(obj==null){
            throw new MyException(message);
        }
    }
    public static void assertNotNull(Object obj, ErrorCode code){
        if(obj==null){
            throw new MyException(code.getMessage(),code.getCode());
        }
    }
    public static void assertListNotNull(List<?> list, String message){
        if(list==null || list.size()<1){
            throw new MyException(message);
        }
    }
    public static void assertListNotNull(List<?> list, ErrorCode code){
        if(list==null || list.size()<1){
            throw new MyException(code.getMessage(),code.getCode());
        }
    }
    public static void assertStrNotBlank(String str,String message){
        if(str==null || str.trim().equals("")){
            throw new MyException(message);
        }
    }
    public static void assertStrNotBlank(String str, ErrorCode code){
        if(str==null || str.trim().equals("")){
            throw new MyException(code.getMessage(),code.getCode());
        }
    }
    //断言字符串,对象等为空
    public static void assertNull(Object obj,String message){
        if (obj!=null)
            throw new MyException(message);
    }
    public static void assertStrBlank(String str,String message){
        if (str!=null || !str.trim().equals(""))
            throw new MyException(message);
    }
    public static void assertListNull(List<?> objs,String message){
        if (objs!=null || objs.size()>0)
            throw new MyException(message);
    }
    public static void assertListNull(List<?> objs,ErrorCode code){
        if (objs!=null || objs.size()>0)
            throw new MyException(code.getMessage(),code.getCode());
    }
    //断言字符串相等
    public static void assertStrEq(String str1,String str2,String message){
        assertNotNull(str1,message);
        assertNotNull(str2,message);
        if (!str1.equals(str2))
            throw new MyException(message);
    }
    public static void assertStrEq(String str1, String str2, ErrorCode code){
        assertNotNull(str1,code);
        assertNotNull(str2,code);
        if (!str1.equals(str2))
            throw new MyException(code.getMessage(),code.getCode());
    }
    public static void assertStrEqIgnoreCase(String str1,String str2,String message){
        assertNotNull(str1,message);
        assertNotNull(str2,message);
        if (!str1.equalsIgnoreCase(str2))
            throw new MyException(message);
    }




}
